package com.midou.administration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.midou.administration.bo.CommonResult;
import com.midou.administration.bo.ShangjiaBo;
import com.midou.administration.entity.Business;
import com.midou.administration.entity.Clerk;
import com.midou.administration.entity.Menu;
import com.midou.administration.mapper.BusinessMapper;
import com.midou.administration.mapper.MenuMapper;
import com.midou.administration.service.BusinessService;
import com.midou.administration.utils.QRCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MrChen
 * @since 2021-03-24
 */
@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;

    private String staticPath = System.getProperty("user.dir")+"\\midou-restaurant-administration\\src\\main\\resources\\static\\image";

    //增加商家
    public int createShangJia(Business business){
        return businessMapper.insert(business);
    }

    public Business queryShop(Integer id){
        QueryWrapper qw = new QueryWrapper();
        qw.eq("bu_id",id);
        Business business = businessMapper.selectOne(qw);
        business.setBuLicense(QRCodeUtils.ImageToBase64ByLocal( staticPath+"\\"+ business.getBuLicense()));
        return business;
    }

    /**
     * 查询所有商家信息  商家中包含该商家的推荐推荐菜品
     * @return
     */
    @Override
    public List<Business> hotMenuByBusiness() {
        return this.businessMapper.hotMenuByBusiness();
    }

    //查询指定商家信息
    public List<Business> businessQueryAll(Clerk clerk){
        List<Business> business = businessMapper.businessQueryAll(clerk.getClerkBusinessId(),clerk.getClerkId());
        for (Business business1 : business) {
            if(business1.getBuLicense()!=null) {
                business1.setBuLicense(QRCodeUtils.ImageToBase64ByLocal(staticPath + "\\" + business1.getBuLicense()));
            }
        }
        return business;
    }

    //商家重充值后修改到期时间
    public CommonResult zengjiaTime(ShangjiaBo shangjiaBo){
        Business business=businessMapper.zengjiaTime(shangjiaBo.getBid());
        if(shangjiaBo.getNian()!=null){
            //计算到期时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String rq=sdf.format(business.getBuDueDate());
            String [] time=rq.split("-");
            Calendar cal = Calendar.getInstance();
            cal.set(Integer.parseInt(time[0]),Integer.parseInt(time[1])-1,Integer.parseInt(time[2]));
            cal.add(Calendar.YEAR, shangjiaBo.getNian());
            Date date=cal.getTime();
            int c=business.getBuDueDate().compareTo(new Date());
            if(c==-1){
                System.out.println("现在时间");
                Calendar cal1 = Calendar.getInstance();
                String rq1=sdf.format(new Date());
                String [] time1=rq1.split("-");
                cal1.set(Integer.parseInt(time1[0]),Integer.parseInt(time1[1])-1,Integer.parseInt(time1[2]));
                cal1.add(Calendar.YEAR, shangjiaBo.getNian());
                Date date1=cal1.getTime();
                System.out.println(sdf.format(date1));
                business.setBuDueDate(date1);

            }else {
                System.out.println("旧时间");
                business.setBuDueDate(date);

            }
            System.out.println(sdf.format(date));

        }else {
            //计算到期时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String rq=sdf.format(business.getBuDueDate());
            String [] time=rq.split("-");
            Calendar cal = Calendar.getInstance();
            cal.set(Integer.parseInt(time[0]),Integer.parseInt(time[1])-1,Integer.parseInt(time[2]));
            cal.add(Calendar.MONTH, shangjiaBo.getYue());
            Date date=cal.getTime();
            int c1=business.getBuDueDate().compareTo(new Date());
            if(c1==1){
                System.out.println("旧时间");
                business.setBuDueDate(date);
            }else {
                System.out.println("现在时间");
                Calendar cal1 = Calendar.getInstance();
                String rq1=sdf.format(new Date());
                String [] time1=rq1.split("-");
                cal1.set(Integer.parseInt(time1[0]),Integer.parseInt(time1[1])-1,Integer.parseInt(time1[2]));
                cal1.add(Calendar.MONTH, shangjiaBo.getYue());
                Date date1=cal1.getTime();
                System.out.println(sdf.format(date1));
                business.setBuDueDate(date1);

            }
            System.out.println(sdf.format(date));
            business.setBuDueDate(date);
        }

        Integer num=businessMapper.updateById(business);
        if(num>0){
            return new CommonResult(200,"充值成功");
        }
        return new CommonResult(500,"请重试");

    }
    //修改商家信息
    public CommonResult updateShangjia(Business business){
        business.setBuLicense(null);
        Integer num=businessMapper.updateById(business);
        if(num>0){
            return new CommonResult(200,"修改成功");
        }else {
            return new CommonResult(500,"修改失败");
        }
    }

    //
    public String doTokenFindShopName(Integer id){
        String buName = this.businessMapper.doTokenFindShopName(id);
        return buName;
    }

    //修改商户头像
    public Integer imgUpload(Business business){
        Integer num=businessMapper.updateById(business);
        if(num>0){
            boolean whetherSucceed = QRCodeUtils.GenerateImage(business.getImgBase64(), staticPath + "/" + business.getBuLicense(), business.getBuLicense());
            if(whetherSucceed){
                return num;
            }
        }
        business.setBuLicense(null);
        return 0;
    }



    }
